export default {

  //开启命名空间
  namespaced: true,

  //数据
  state: () => ({

    //收货地址：读取本地存储的收货地址，初始化address对象
    address: JSON.parse(uni.getStorageSync('address') || '{}')
  }),

  //方法
  mutations: {

    //更新收货地址
    updateAddress(state, address) {
      console.log(address)
      state.address = address
      
      //调用this.commit ，持久化存储到本地
      this.commit('m_user/saveAddressToStorage')
    },
    saveAddressToStorage(state,address){
      uni.setStorageSync('address', JSON.stringify(state.address))
    }
  },

  getters: {
    //将addstr抽离为getters
    //目的：为了提高代码的复用性，可以把收货的详细地址抽离为getters，方便在多个页面和组件之间实现复用。
    addstr (state){
      if(!state.address.provinceName) return ''
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }

}